package com.ojoin.trade.common.utils.paynow;

import java.util.Map;

import com.ojoin.trade.common.utils.Config;
import com.ojoin.trade.common.utils.MD5;

/**
 * Created with IntelliJ IDEA.
 * User: Twar3
 * Date: 15-1-2
 * Time: 下午12:12
 * To change this template use File | Settings | File Templates.
 */
public class Packages {


    /**
     * 返回串
     * @param funcode 机构号
     * @param dataMap 返回信息
     * @return
     */
    public String returnMess(String funcode, Map dataMap) {
        StringBuilder return_mess = new StringBuilder();
        
        String DSEKey = Config.getProperty("ipaynow.dsekey");//3DES需要的key
        String MD5Key = Config.getProperty("ipaynow.md5key");//MD5需要的key

        String srt1;
        String srt2;
        String srt3;

        String dataStr = FormDateReportConvertor.postFormLinkReport(dataMap);//参数数据
        String joinorgcode=Config.getProperty("ipaynow.joinorgcode");
        srt1 = this.base("funcode="+funcode+"&orgId="+joinorgcode) ;

        srt2 = ThreeDESEncryptProvider.encrypt(dataStr,DSEKey);
        srt2 = this.base(srt2);

        srt3 = this.MD5Message(dataStr, MD5Key);
        srt3 = this.base(srt3);

        return_mess.append(srt1);
        return_mess.append("|");
        return_mess.append(srt2); //拼接返回报文
        return_mess.append("|");
        return_mess.append(srt3);

        return return_mess.toString();
    }

    /**
     * 将数据进行base64加密
     * @param dataStr 数据
     * @return
     */
    private String base(String dataStr){
        String return_mess = "";
        return_mess = BASE64.encode(dataStr.getBytes());
        return return_mess;
    }

    /**
     * 生成MD5签名
     * @param dataStr 数据
     * @param MD5Key MD5 key
     * @return
     */
    private String MD5Message(String dataStr, String MD5Key){
        String signNow ;
        try {
            if(dataStr==null || "".equals(dataStr)){
                return "";
            }

            signNow = MD5.md5(dataStr + MD5Key, "utf-8");

        }catch (Exception e){
            return "";
        }
        return signNow;
    }

}
